package cn.casc.tools.er;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * @author max
 * @version 1.0.1
 * @className ColumnTable
 * @description
 * @copyright Copyright (c) 2023
 * @company 天纲梦工厂
 * @datetime 2024/7/19 14:16
 **/

public class ColumnTable extends JTable {
    static String[] columnNames = new String[]{"#","名称","类型","备注", "外键"};
    List<ColumnItem> data;
    public ColumnTable(List<ColumnItem> data) {
        super(new AbstractTableModel() {
            public String getColumnName(int column) { return columnNames[column]; }
            public int getRowCount() { return data.size(); }
            public int getColumnCount() { return columnNames.length; }
            public Object getValueAt(int row, int col) {
                switch (col) {
                    case 0:
                        return row+1;
                    case 1:
                        return data.get(row).column;
                    case 2:
                        return data.get(row).columnType;
                    case 3:
                        return data.get(row).columnComment;
                    case 4:
                        ColumnItem ci = data.get(row);
                        StringBuilder sb = new StringBuilder();
                        for(EntityNode other:ci.typeToOther.values()) {
                            sb.append(String.format("-> %s\n", other.id));
                        }
                        for(EntityNode other:ci.maybe) {
                            sb.append(String.format("? %s\n", other.id));
                        }
                        return sb.toString();
                }
                return "";
            }
            public boolean isCellEditable(int row, int column) {
                if(column == 4) {
                    return true;
                }
                return false;
            }
            public void setValueAt(Object value, int row, int col) {
                if(col == 4) {
                    data.get(row).columnComment = (String)value;
                    fireTableCellUpdated(row, col);
                }
            }

        });
        this.data = data;

        int width = this.tableHeader.getWidth() - 30;


        this.getColumn(columnNames[0]).setMaxWidth(30);
        this.getColumn(columnNames[1]).setMaxWidth(220);
        this.getColumn(columnNames[1]).setMinWidth(150);
        this.getColumn(columnNames[2]).setMaxWidth(180);
    }

    public void updateData() {
        for(int i = 0; i < data.size(); i ++) {

            for(int col = 0; col < columnNames.length; col ++) {
                //                super.dataModel..fireTableCellUpdated(i, col);
            }
        }
    }
}
